package atm;

import IO.Test01;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class AtmGUI extends JFrame {
    private ArrayList users;
    public static final String FILE_PATH  = "users.dat";
    private User loginUser = new User();
    public int index;
    private Image bgImg = new ImageIcon("images\\background.jpg").getImage();
    private CardLayout cardLayout = new CardLayout();
    private LoginPanel loginPanel = new LoginPanel(this);
    private MenuPanel menuPanel = new MenuPanel(this);
    private GetPanel getPanel = new GetPanel(this);
    private ChangePwdPanel changePwdPanel = new ChangePwdPanel(this);
    private SavePanel savePanel = new SavePanel(this);
    private SearchPanel searchPanel = new SearchPanel(this);
    private TransferPanel transferPanel = new TransferPanel(this);


    public AtmGUI(){
        this.init();
        this.setContentPane(new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(bgImg,0,0,this);
            }
        });

        this.setLayout(cardLayout);
        this.add(loginPanel,"login");
        this.add(menuPanel,"menu");
        this.add(getPanel,"get");
        this.add(changePwdPanel,"changePwd");
        this.add(savePanel,"save");
        this.add(searchPanel,"search");
        this.add(transferPanel,"transfer");

        this.setTitle("ATM");
        this.setSize(1024,768);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    /**
     * 初始化用户
     */
    public void init(){
        users = (ArrayList) FileUtil.readObject(FILE_PATH);
        if(users == null){
            users = new ArrayList<>();
            users.add(new User("aaaaaa","111111",1000));
            users.add(new User("bbbbbb","222222",2000));
            users.add(new User("cccccc","333333",3000));
            users.add(new User("dddddd","444444",4000));
            users.add(new User("eeeeee","555555",5000));
            FileUtil.writeObject(users,FILE_PATH);
        }

    }

    public ArrayList<User> getUsers() {
        return users;
    }

    public void setUsers(ArrayList users) {
        this.users = users;
    }

    public void showPanel(String name){
        cardLayout.show(this.getContentPane(),name);
    }
    public static void main(String[] args) {
        new AtmGUI();
    }

    public User getLoginUser() {
        return loginUser;
    }

    public void setLoginUser(User loginUser) {
        this.loginUser = loginUser;
    }
}
